# coding=utf-8
import time

from django.conf import settings


def upload_file(file):
    parts = file.name.rsplit(".", maxsplit=1)
    if len(parts) == 1:
        parts.append("")
    file_name = "{0}_{1}.{2}".format(
        parts[0], int(time.time() * 1000), parts[1])
    full_path = "{0}{1}".format(
        settings.IMAGE_UPLOAD_PATH, file_name)
    with open(full_path, "wb+") as f:
        while True:
            chunk = file.read(1024)
            if not chunk:
                break
            f.write(chunk)
    full_url = "{0}{1}".format(
        settings.IMAGE_UPLOAD_URL, file_name)
    return full_url
